package io.renren.modules.erp.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.renren.common.constants.ExceptionConstants;
import io.renren.common.exception.BusinessParamCheckingException;
import io.renren.common.exception.BusinessRunTimeException;
import io.renren.common.utils.Constants;
import io.renren.common.utils.R;
import io.renren.common.utils.StringUtil;
import io.renren.modules.erp.entity.SerialNumberEx;
import io.renren.modules.erp.service.SerialNumberService;
import io.renren.modules.sys.controller.AbstractController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Description
 *
 * @Author: cjl
 * @Date: 2019/1/22 10:29
 */
@RestController
@RequestMapping(value = "/serialNumber")
public class SerialNumberController extends AbstractController {
    private Logger logger = LoggerFactory.getLogger(SerialNumberController.class);

    @Resource
    private SerialNumberService serialNumberService;
    @GetMapping("/list")
    public R getSerialNumberList(@RequestParam Map<String, String> params) throws Exception{
        String search = params.get(Constants.SEARCH);

        String serialNumber = StringUtil.getInfo(search, "serialNumber");
        String materialName = StringUtil.getInfo(search, "materialName");
        int pageSize = Integer.valueOf(params.get( "pageSize"));
        int currPage = Integer.valueOf(params.get( "currentPage"));
        return R.ok().put("data",serialNumberService.select(serialNumber,materialName,currPage, pageSize));
    }
    /**
     * create by: cjl
     * description:
     *  检查序列号是否存在
     * create time: 2019/1/22 11:02
     * @Param: id
     * @Param: materialName
     * @Param: serialNumber
     * @Param: request
     * @return java.lang.Object
     */
    @PostMapping("/checkIsExist")
    public R checkIsExist(@RequestParam("id") Long id, @RequestParam("materialName") String materialName,
                          @RequestParam("serialNumber") String serialNumber, HttpServletRequest request) throws Exception{
        if(StringUtil.isEmpty(serialNumber)){
            throw new BusinessParamCheckingException(ExceptionConstants.SERIAL_NUMBERE_NOT_BE_EMPTY_CODE,
                    ExceptionConstants.SERIAL_NUMBERE_NOT_BE_EMPTY_MSG);
        }
        serialNumberService.checkIsExist(id, materialName, serialNumber);
        return R.ok();
    }
    /**
     * create by: cjl
     * description:
     *  新增序列号信息
     * create time: 2019/1/22 17:10
     * @Param: beanJson
     * @Param: request
     * @return java.lang.Object
     */
    @PostMapping("/addSerialNumber")
    public R addSerialNumber(@RequestBody SerialNumberEx sne)throws Exception{
        serialNumberService.addSerialNumber(sne,getUserId());
        return R.ok();

    }
    /**
     * create by: cjl
     * description:
     *  修改序列号信息
     * create time: 2019/1/23 13:56
     * @Param: beanJson
     * @return java.lang.Object
     */
    @PostMapping("/updateSerialNumber")
    public R updateSerialNumber(@RequestBody SerialNumberEx serialNumberEx)throws Exception{
        serialNumberService.updateSerialNumber(serialNumberEx,getUserId());
        return R.ok();

    }
    /**
     * create by: cjl
     * description:
     *批量添加序列号
     * create time: 2019/1/29 15:11
     * @Param: materialName
     * @Param: serialNumberPrefix
     * @Param: batAddTotal
     * @Param: remark
     * @return java.lang.Object
     */
    @PostMapping("/batAddSerialNumber")
    public R batAddSerialNumber(@RequestParam("materialName") String materialName, @RequestParam("serialNumberPrefix") String serialNumberPrefix,
                                @RequestParam("batAddTotal") Integer batAddTotal,@RequestParam("remark") String remark)throws Exception{

        serialNumberService.batAddSerialNumber(materialName,serialNumberPrefix,batAddTotal,remark,getUserId());
        return R.ok();

    }
    /**
     * create by: qiankunpingtai
     * website：https://qiankunpingtai.cn
     * description:
     *  逻辑删除序列号信息
     * create time: 2019/3/27 17:43
     * @Param: ids
     * @return java.lang.Object
     */
    @RequestMapping(value = "/batchDeleteSerialNumberByIds")
    public R batchDeleteSerialNumberByIds(@RequestParam("ids") String ids) throws Exception {
        int i= serialNumberService.batchDeleteSerialNumberByIds(ids,getUserId());
        if(i<1){
            logger.error("异常码[{}],异常提示[{}],参数,ids[{}]",
                    ExceptionConstants.SERIAL_NUMBERE_DELETE_FAILED_CODE,ExceptionConstants.SERIAL_NUMBERE_DELETE_FAILED_MSG,ids);
            throw new BusinessRunTimeException(ExceptionConstants.SERIAL_NUMBERE_DELETE_FAILED_CODE,
                    ExceptionConstants.SERIAL_NUMBERE_DELETE_FAILED_MSG);
        }
        return R.ok();
    }

    @PostMapping(value = "/add")
    public R insert(@RequestBody String beanJson, HttpServletRequest request)throws Exception {
        if(1==serialNumberService.insertSerialNumber(beanJson, request)){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @PostMapping(value = "/update")
    public R update(@RequestBody String beanJson,@RequestParam("id")Long id, HttpServletRequest request)throws Exception {
        if(1==serialNumberService.updateSerialNumber(beanJson, id, request)){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @GetMapping(value = "/delete")
    public R delete(@RequestParam("id") Long id, HttpServletRequest request)throws Exception {
        if(1==serialNumberService.deleteSerialNumber(id, request)){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @GetMapping(value = "/batchDelete")
    public R batchDelete(@RequestParam("ids") String ids, HttpServletRequest request)throws Exception {
        if(1==serialNumberService.batchDeleteSerialNumber(ids, request)){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @GetMapping(value = "/checkIsNameExist")
    public R checkIsNameExist(@RequestParam("id") Long id, @RequestParam("serialNumber")String serialNumber) throws Exception{
        if(0<serialNumberService.checkIsNameExist(id, serialNumber)){
            return R.ok();
        }else{
            return R.error();
        }
    }


}
